home *** CD-ROM | disk | FTP | other *** search
/ Windows Expert / Windows Expert.iso / windownt / uupc11ys.zip / LIB / KANJICNV.C < prev    next >
C/C++ Source or Header  |  1992-11-27  |  6KB  |  175 lines

  1. /*
  2.  * kanjicnv.c
  3.  * Japanese conversion routines for UUPC/Extended
  4.  *
  5.  * Based on Oimo source file stdfunc.c
  6.  * Programmed by Shigeki Matsushima and Dai Yokota 4-MAY-1989
  7.  * Modified by Kenji Rikitake 13-AUG-1991
  8.  * Copyright (c) 1988, 1991 by Shigeki Matsushima, Dai Yokota, and
  9.  * Kenji Rikitake. All rights reserved.
  10.  *
  11.  */
  12.  
  13. /*--------------------------------------------------------------------*/
  14. /*                        System include files                        */
  15. /*--------------------------------------------------------------------*/
  16.  
  17. #include <stdio.h>
  18. #include <time.h>
  19.  
  20. /*--------------------------------------------------------------------*/
  21. /*                    UUPC/extended include files                     */
  22. /*--------------------------------------------------------------------*/
  23.  
  24. #include "lib.h"
  25. #include "kanjicnv.h"
  26.  
  27. /*
  28.  * On Japanese support:
  29.  *
  30.  * Japanese MS-DOS uses a 2byte Kanji (Japanese ideogram) code called
  31.  * "Shift-JIS". This cannot be delivered via SMTP since Shift-JIS maps
  32.  * its first byte from 0x80-0x9f and 0xe0-0xfc.
  33.  * JUNET requests all hosts to send Kanji in a 7bit subset of ISO2022.
  34.  * This is commonly called "JIS 7bit".
  35.  *
  36.  * To provide Japanese functionality, you need to convert
  37.  * all remote delivery messages to JIS 7bit, and
  38.  * all local delivery messages to Shift-JIS.
  39.  */
  40.  
  41. /*--------------------------------------------------------------------*/
  42. /*                           Global defines                           */
  43. /*--------------------------------------------------------------------*/
  44.  
  45. #define SEQ_TO_JIS90 "\033$B"
  46. #define SEQ_TO_ASCII "\033(B"
  47.  
  48. /*--------------------------------------------------------------------*/
  49. /*                               Macros                               */
  50. /*--------------------------------------------------------------------*/
  51.  
  52. /* check if c is the first byte of Shift-JIS Kanji */
  53.  
  54. #define  iskanji(c)  ((unsigned char)(c) >= 0x81 && \
  55.           (unsigned char)(c) <= 0x9f || \
  56.           (unsigned char)(c) >= 0xe0 && \
  57.           (unsigned char)(c) <= 0xfc)
  58.  
  59. /* check if c is the second byte of Shift-JIS Kanji */
  60.  
  61. #define  iskanji2(c) ((unsigned char)(c) >= 0x40 && \
  62.           (unsigned char)(c) <= 0x7e || \
  63.           (unsigned char)(c) >= 0x80 && \
  64.           (unsigned char)(c) <= 0xfc)
  65.  
  66. /*--------------------------------------------------------------------*/
  67. /*    f p u t s _ s h i f t j i s                                     */
  68. /*                                                                    */
  69. /*    fputs() with conversion from JIS 7bit to Shift-JIS              */
  70. /*--------------------------------------------------------------------*/
  71.  
  72. int fputs_shiftjis(unsigned char *buf, FILE *fp)
  73. {
  74.    int shiftin = FALSE;
  75.    unsigned char hi, lo;
  76.  
  77.    while (*buf) {
  78.       if ((*buf == '\033') && (*(buf+1) == '$') &&
  79.          ((*(buf+2) == 'B') || (*(buf+2) == '@'))) {
  80.          shiftin = TRUE;
  81.          buf += 3;
  82.          }
  83.       else if ((*buf == '\033') && (*(buf+1) == '(')
  84.           && ((*(buf+2) == 'J') || (*(buf+2) == 'B') ||
  85.               (*(buf+2) == 'H'))) {
  86.          shiftin = FALSE;
  87.          buf += 3;
  88.          }
  89.       else if (shiftin) {
  90.          hi = *buf++;
  91.          if ((lo = *buf++) == '\0')
  92.             break;
  93.          if (hi & 1) lo += 0x1f;
  94.          else lo += 0x7d;
  95.          if (lo >= 0x7f) lo++;
  96.          hi = (hi - 0x21 >> 1) + 0x81;
  97.          if (hi > 0x9f) hi += 0x40;
  98.          if (EOF == fputc(hi, fp)) {
  99.             return EOF;
  100.             }
  101.          if (EOF == fputc(lo, fp)) {
  102.             return EOF;
  103.             }
  104.          }
  105.       else {
  106.          if (EOF == fputc(*buf, fp)) {
  107.             return EOF;
  108.             }
  109.          buf++;
  110.          }
  111.       }
  112.    return 0;
  113. } /* fputs_shiftjis */
  114.  
  115. /*--------------------------------------------------------------------*/
  116. /*    fputs_jis7bit                                                   */
  117. /*                                                                    */
  118. /*    fputs() with conversion from Shift-JIS to JIS 7bit              */
  119. /*--------------------------------------------------------------------*/
  120.  
  121. int fputs_jis7bit(unsigned char *buf, FILE *fp)
  122. {
  123.    int kanjiflag = FALSE;
  124.    unsigned char hi, lo;
  125.    int i;
  126.  
  127.    while (*buf) {
  128.       if (iskanji(*buf) && iskanji2(*(buf+1))) {
  129.          if (kanjiflag == FALSE) {
  130.             kanjiflag = TRUE;
  131.             if (0 != (i = fputs(SEQ_TO_JIS90, fp))) {
  132.                return i;
  133.                }
  134.             }
  135.          hi = *buf++;
  136.          if ((lo = *buf++) == '\0')
  137.             break;
  138.          hi -= (hi <= 0x9f) ? 0x71 : 0xb1;
  139.          hi = hi * 2 + 1;
  140.          if (lo > 0x7f) lo -= 1;
  141.          if (lo >= 0x9e) {
  142.             lo -= 0x7d;
  143.             hi += 1;
  144.             }
  145.          else {
  146.             lo -= 0x1f;
  147.             }
  148.          if (EOF == fputc(hi, fp)) {
  149.             return EOF;
  150.             }
  151.          if (EOF == fputc(lo, fp)) {
  152.             return EOF;
  153.             }
  154.          }
  155.       else {
  156.          if (kanjiflag == TRUE) {
  157.             kanjiflag = FALSE;
  158.             if (0 != (i = fputs(SEQ_TO_ASCII, fp))) {
  159.                return i;
  160.                }
  161.             }
  162.          if (EOF == fputc(*buf, fp)) {
  163.             return EOF;
  164.             }
  165.          buf++;
  166.          }
  167.       }
  168.    if (kanjiflag) {
  169.       if (0 != (i = fputs(SEQ_TO_ASCII, fp))) {
  170.          return i;
  171.          }
  172.       }
  173.    return 0;
  174. } /* fputs_jis7bit */
  175.